package cn.kgc.blog.controller;

import cn.kgc.blog.pojo.Blog;
import cn.kgc.blog.service.BlogService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class BlogController {
    @Resource
    private BlogService blogService;
    @RequestMapping("/blogquery.do")
    public String blogselect(int userid, @RequestParam(required = false,defaultValue = "0") int pageIndex, Model model){
        if(pageIndex==0) {
             pageIndex = 1;
        }
        int pageSize=2;
        PageHelper.startPage(pageIndex,pageSize);
        List<Blog> blogList = blogService.selectAll(userid);
        PageInfo<Blog> info=new PageInfo<Blog>(blogList);
        model.addAttribute("info",info);
        model.addAttribute("bloglist",blogList);
        return "blogquery";
    }
    @RequestMapping("/add.do")
    public String addblog(String content, @RequestParam("userid") String userid, Model model, HttpSession session){
    Blog blog=new Blog();
    blog.setContent(content);
    int a=0;
    if(userid!=null){
        a=Integer.parseInt(userid);
    }
    blog.setUserid(a);
        int add = blogService.add(blog);
        if(add>0){
            model.addAttribute("msg","添加成功!");
            return "addblog";
        }else{
            model.addAttribute("msg","失败");
            return "addblog";
        }
    }
    @RequestMapping("/deleteblog")
    public String delete(@RequestParam("id") int id,@RequestParam("userid") int userid){
        int a=blogService.delete(id,userid);
        String aa="redirect:/blogquery.do?userid="+userid;
        return aa;
    }
}
